<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This class is representing a XML feed similar to this:
 * <generator uri="http://madebymonsieur.com" version="0.0">MiniMe</generator>
 *  
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Atom_Feed_Generator implements Atom_ChildElement {
    
    private $uri;
    private $version;
    private $value;
    
    const VERSION = "0.0.1";
    
    const ELEMENT_GENERATOR = "generator";
    
    const ATTRIBUTE_URI  = "uri";
    const ATTRIBUTE_VERSION = "version";
    
    public function __construct($uri, $value, $version = self::VERSION) {
        $this->setUri($uri);
        $this->setValue($value);
        $this->setVersion($version);
    }
    
    private function setUri($uri) {
        if(empty($uri)) {
            throw new Exception("Uri cannot be set empty.");
        }
        $this->uri = $uri;
    }
    
    private function setValue($value) {
        if(empty($value)) {
            throw new Exception("Value cannot be set empty.");
        }
        $this->value = $value;
    }
    
    private function setVersion($version) {
//        if(empty($version)) {
//            throw new Exception("Version cannot be set empty.");
//        }
        $this->version = $version;
    }
    
	/**
	 * This should return element similar to:
	 * <generator uri="http://madebymonsieur.com" version="0.0">MiniMe</generator>
	 * 
     * @return DOMElement
     */
    public function asDomElement(DOMDocument $dom) {
        
        $element = $dom->createElement(self::ELEMENT_GENERATOR, $this->value);
        
        $element->setAttribute(self::ATTRIBUTE_URI, $this->uri);
        $element->setAttribute(self::ATTRIBUTE_VERSION, $this->version);
        
        return $element;
    }
    
    /**
     * @param DOMElement $elm
     * @return Atom_Feed_Generator
     */
    public static function buildFromDOMElement(DOMElement $elm) {
        if($elm == null) {
            return null;
        }
        $value   = $elm->nodeValue;
        $uri     = $elm->getAttribute(self::ATTRIBUTE_URI);
        $version = $elm->getAttribute(self::ATTRIBUTE_VERSION);
        
        return new Atom_Feed_Generator($uri, $value, $version);
    }
}